今天开始,更新 Powershell 入门系列。第一次参加铁人赛,C# 系列的文章写了十几篇了,感觉在没有详细的规划下,写一个系列的文章,非常不容易,经常会碰到,写到后面内容,涉及到一些基础的内容,但在你前面的文章里,并没有提到。
由于是第一篇,我们先来简单介绍一下 Powershell 吧。 Powershell 是微软在 Windows 7/Windows Server 2008 里面添加的进来的,开始的时候,并不怎么好用,所以初期使用 Powershell 的人并不多,但从 Windows 8/Windows Server 2012 开始,这一现象改变了,Powershell 从一个简单的命令行工具变成了一种自动化的语言。下面我们就来简单看一下 Powershell。
你可以通过开始菜单,打开 Powershell 或 Powershell ISE。Powershell ISE 是一个交互式的 Powershell 环境,对于初学者,或者写脚本时,Powershell ISE 是一个不错的选择。
你可以通过右击 Powershell 窗口上边的边栏,右击选择 Properties,以设置 Powershell 窗口的属性。比如,设置窗口大小,背景,字体,以及字体的大小:
调整好合适的背景,字体后,我们来一起看一下 Powershell 的使用吧。 Powershell 的使用,和之前的 CMD 没有多大区别,但 Powershell 支持的命令更多。在命令行窗口输入 “$PSVersionTable” 查看 Powershell 的版本,我当前使用的 Powershell 版本是 5.1 的。
PS C:\Users\Administrator> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.17763.1971
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.1971
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
我们可以通过 get-help 命令,获取命令相关的帮助:
PS C:\Users\Administrator> get-help test-connection
Do you want to run Update-Help?
The Update-Help cmdlet downloads the most current Help files for Windows PowerShell modules, and installs them on your
computer. For more information about the Update-Help cmdlet, see https:/go.microsoft.com/fwlink/?LinkId=210614.
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): y
NAME
Test-Connection
SYNOPSIS
Sends ICMP echo request packets, or pings, to one or more computers.
SYNTAX
Test-Connection [-ComputerName] <System.String[]> [-AsJob] [-BufferSize <System.Int32>] [-Count <System.Int32>]
[-DcomAuthentication {Default | None | Connect | Call | Packet | PacketIntegrity | PacketPrivacy | Unchanged}]
[-Delay <System.Int32>] [-Impersonation {Default | Anonymous | Identify | Impersonate | Delegate}] [-Protocol
{DCOM | WSMan}] [-ThrottleLimit <System.Int32>] [-TimeToLive <System.Int32>] [-WsmanAuthentication {Default |
Basic | Negotiate | CredSSP | Digest | Kerberos}] [<CommonParameters>]
Test-Connection [-ComputerName] <System.String[]> [-Source] <System.String[]> [-AsJob] [-BufferSize
<System.Int32>] [-Count <System.Int32>] [-Credential <System.Management.Automation.PSCredential>]
[-DcomAuthentication {Default | None | Connect | Call | Packet | PacketIntegrity | PacketPrivacy | Unchanged}]
[-Delay <System.Int32>] [-Impersonation {Default | Anonymous | Identify | Impersonate | Delegate}] [-Protocol
{DCOM | WSMan}] [-ThrottleLimit <System.Int32>] [-TimeToLive <System.Int32>] [-WsmanAuthentication {Default |
Basic | Negotiate | CredSSP | Digest | Kerberos}] [<CommonParameters>]
Test-Connection [-ComputerName] <System.String[]> [-BufferSize <System.Int32>] [-Count <System.Int32>]
[-DcomAuthentication {Default | None | Connect | Call | Packet | PacketIntegrity | PacketPrivacy | Unchanged}]
[-Delay <System.Int32>] [-Impersonation {Default | Anonymous | Identify | Impersonate | Delegate}] [-Protocol
{DCOM | WSMan}] [-Quiet] [-TimeToLive <System.Int32>] [-WsmanAuthentication {Default | Basic | Negotiate | CredSSP
| Digest | Kerberos}] [<CommonParameters>]
DESCRIPTION
The `Test-Connection` cmdlet sends Internet Control Message Protocol (ICMP) echo request packets, or pings, to one
or more remote computers and returns the echo response replies. You can use this cmdlet to determine whether a
particular computer can be contacted across an IP network.
You can use the parameters of `Test-Connection` to specify both the sending and receiving computers, to run the
command as a background job, to set a time-out and number of pings, and to configure the connection and
authentication.
Unlike the familiar ping command, `Test-Connection` returns a Win32_PingStatus object that you can investigate in
PowerShell. The Quiet parameter returns a Boolean value in a System.Boolean object for each tested connection. If
multiple connections are tested, an array of Boolean values is returned.
RELATED LINKS
Online Version: https://docs.microsoft.com/powershell/module/microsoft.powershell.management/test-connection?view=p
owershell-5.1&WT.mc_id=ps-gethelp
Add-Computer
Restart-Computer
Stop-Computer
REMARKS
To see the examples, type: "get-help Test-Connection -examples".
For more information, type: "get-help Test-Connection -detailed".
For technical information, type: "get-help Test-Connection -full".
For online help, type: "get-help Test-Connection -online"
当你你第一次查看命令帮助时,会有一个提示,是否使用 update-help 从互联网更新帮助,这里一般选择 Y/y 即可,系统会自动从网上更新帮助。你也可以使用帮助内容最后的 REMARKS 获取相关类型的帮助文档。
当你觉得屏幕内容太多,有点乱的时候,你可以通过 cls 清理屏幕。
对于比较长的命令,我们只需要记住开始就可以了,然后通过 tab 键可以自动补全。比如,我们在 Powershell 窗口中输入 “test-con” 然后按 tab 键,就可以自动补全命令 “Test-Connection”。
但有些时候,我们并不知道具体命令是什么,只知道命令的部分,如,我不记得创建用户的命令是什么了,但我知道,这个命令和 user 相关,我们可以通过 get-command 获取完整的命令(* 表示任意数量的任意字符。):
PS C:\Users\Administrator> Get-Command *user*
CommandType Name Version Source
----------- ---- ------- ------
Function Disable-ServerManagerStandardUserRemoting 2.0.0.0 ServerManager
Function Disconnect-RDUser 2.0.0.0 RemoteDesktop
Function Enable-ServerManagerStandardUserRemoting 2.0.0.0 ServerManager
Function Get-RDUserSession 2.0.0.0 RemoteDesktop
Function Get-UalDailyUserAccess 1.0.0.0 UserAccessLogging
Function Get-UalServerUser 1.0.0.0 UserAccessLogging
Function Get-UalUserAccess 1.0.0.0 UserAccessLogging
Function Invoke-RDUserLogoff 2.0.0.0 RemoteDesktop
Function Send-RDUserMessage 2.0.0.0 RemoteDesktop
Function Set-PcsvDeviceUserPassword 1.0.0.0 PcsvDevice
Cmdlet Disable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Enable-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Get-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Get-WinUserLanguageList 2.0.0.0 International
Cmdlet New-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet New-WinUserLanguageList 2.0.0.0 International
Cmdlet Remove-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Rename-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Restore-UevUserSetting 2.1.639.0 UEV
Cmdlet Set-LocalUser 1.0.0.0 Microsoft.PowerShell.LocalAccounts
Cmdlet Set-WinUserLanguageList 2.0.0.0 International
Application DevModeRunAsUserConfig.msc 0.0.0.0 C:\Windows\system32\DevModeRunAsUserCo...
Application DsmUserTask.exe 10.0.17... C:\Windows\system32\DsmUserTask.exe
Application quser.exe 10.0.17... C:\Windows\system32\quser.exe
Application UserAccountBroker.exe 10.0.17... C:\Windows\system32\UserAccountBroker.exe
Application UserAccountControlSettings.exe 10.0.17... C:\Windows\system32\UserAccountControl...
Application userinit.exe 10.0.17... C:\Windows\system32\userinit.exe